<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/


class JVInstall {	
	public static function process(JInstallerComponent &$component) {
       	
		if (isset($component->get('manifest')->additional[0])) {
			$add = $component->get('manifest')->additional[0];
			if (count($add->children())) {
			    $exts = $add->children();
			    foreach ($exts as $ext) {
					$installer = new JInstaller();
					$installer->setOverwrite(true);
					
					$update = false;
					if ($ext->name() == 'template' && JFolder::exists(JPATH_ROOT.'/templates/'.$ext->getAttribute('name'))) {
						$update = true;
					}
                    
                    if ($ext->name() == 'library' && JFolder::exists(JPATH_ROOT.'/libraries/'.$ext->getAttribute('name'))) {
						$update = true;
					}
					
					if ($ext->name() == 'component' && JFolder::exists(JPATH_ROOT.'/components/'.$ext->getAttribute('name'))) {
						$update = true;
					}
		
					$folder = $component->get('parent')->getPath('source').'/'.$ext->getAttribute('folder');					
					$folder = rtrim($folder, "\\/") . '/';
                    
                    if($ext->name() == 'template'){
                        $templateDetails = $folder.DS.'templateDetails.xml';
                        $xml	=& JFactory::getXMLParser('Simple');
                        $xml    ->loadFile($templateDetails);
                        $themes = $xml->document->getElementByPath( 'themes' )->theme;
                        $home   = self::hasdefault();
                        $installed  = (array) self::getInstalledTheme();
						
                        if(count($themes)){
                            foreach($themes as $theme){
                               if(!in_array($theme->data(), $installed)){
                                    $db  = JFactory::getDBO();
									$sql = "INSERT INTO #__jv_themes value(NULL, '".$theme->data()."', ".((bool)$theme->attributes('isdefault') && $home == '' ? 1:0).", '', 0)";
									$db->setQuery($sql); 
									$db->query();
								}
                            }
                        }
                    }
                    
					if (JFolder::exists($folder)) {
				    	$extensions[] = array(
							'name' => $ext->data(),
							'type' => $ext->name(),
							'folder' => $folder,
							'installer' => $installer,
							'status' => false,
				    		'update' => $update
				    	);
				    }
			    }
			}	
		}
		
		// install additional extensions
		for ($i = 0; $i < count($extensions); $i++) {
			if (is_dir($extensions[$i]['folder'])) {
				if (@$extensions[$i]['installer']->install($extensions[$i]['folder'])) {
					$extensions[$i]['status'] = $extensions[$i]['update'] ? 2 : 1;
				} else {
					$error = true;
					break;
				}
			}
		}
        
        $db  = JFactory::getDBO();
        $sql = "UPDATE #__extensions SET enabled=1 WHERE type='plugin' AND element='jvframework'";
        $db->setQuery($sql);
        $db->query();
        
		self::displayResults($extensions, 'Extensions', 'Extension');
		
		return true;
	}
	
	function hasdefault(){
        $db  = JFactory::getDBO();
        $sql = "SELECT theme FROM #__jv_themes WHERE home=1";
        $db->setQuery($sql);
        $result = $db->loadResult();
        
        return $result;
    }
    
    function getInstalledTheme(){
        $db  = JFactory::getDBO();
        $sql = "SELECT theme FROM #__jv_themes";
        $db->setQuery($sql);
        $result = $db->loadResultArray();
        
        return $result;
    }
	
	public static function displayResults($result, $name, $type) {?>
		
		<h3><?php echo JText::_($name); ?></h3>
		<table class="adminlist">
			<thead>
				<tr>
					<th class="title"><?php echo JText::_($type); ?></th>
					<th width="60%"><?php echo JText::_('Status'); ?></th>
				</tr>
			</thead>
			<tfoot>
				<tr>
					<td colspan="2">&nbsp;</td>
				</tr>
			</tfoot>
			<tbody>
				<?php 
					foreach ($result as $i => $ext) : ?>
					<tr class="row<?php echo $i++ % 2; ?>">
						<td class="key"><?php echo $ext['name']; ?></td>
						<td>
							<?php $style = $ext['status'] ? 'font-weight: bold; color: green;' : 'font-weight: bold; color: red;'; ?>
							<?php $update = $ext['status'] == 2 ? 'Updated' : 'Installed'; ?>
							<span style="<?php echo $style; ?>"><?php echo $ext['status'] ? JText::_($update.' successfully') : JText::_('NOT Installed'); ?></span>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
		
		<?php
		
	}	
	
}